<?php
include 'config.php';
ini_set('date.timezone','Asia/Shanghai');//调整时区
$hdid1=$config['hdid1'];//活动名称
$hdid2=$config['hdid2'];//活动名称
$time=$config['time'];//时间
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "data";
ini_set('date.timezone','Asia/Shanghai');
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
$sql="select * , SUBSTRING(  `date` , 1, 15 ) AS dateindex from msg order by date asc limit 1";
$result=$conn->query($sql);
$temp= $result->fetch_assoc();
$start=$temp['dateindex'].'0';
$sql="select * , SUBSTRING(  `date` , 1, 15 ) AS dateindex from msg order by date desc limit 1";
$result=$conn->query($sql);
$temp= $result->fetch_assoc();
$end=$temp['dateindex'].'0';
$f=floor((strtotime($end)-strtotime($start))%86400/600);
//echo $start."<br/>";
//echo $end."<br/>";
$now=strtotime($start);
for($i=0;$i<$f;$i++) {
    $_date[$i] = $now;
    $now = $now + 10 * 60;
}

$sql="SELECT * , COUNT( * ) AS count, SUBSTRING(  `date` , 1, 15 ) AS dateindex FROM msg WHERE mtype =10000 and (hdid='$hdid1'or hdid ='$hdid2')  GROUP BY user_id HAVING  SUBSTRING(  `date` , 1, 10 )='$time' order by user_name asc";
$result = $conn->query($sql);
$group=array();
if ($result->num_rows > 0) {
    // 输出每行数据
    while($row = $result->fetch_assoc()) {
        if($row['count']>10){
            $name=$row['user_name'];
            $sql1="SELECT * , COUNT( * ) AS count, SUBSTRING(  `date` , 1, 15 ) AS dateindex FROM msg WHERE mtype =10000 and (hdid='$hdid1'or hdid ='$hdid2')AND  SUBSTRING(  `date` , 1, 10 )='$time'   GROUP BY SUBSTRING(  `date` , 1, 15 )  having user_name='$name'";
           // echo $sql1.'<br/>';
            $res = $conn->query($sql1);
            if ($res->num_rows > 0) {
                // 当前组第一条数据
                $row = $res->fetch_assoc();
                $_time=date("H:i",strtotime( $row['dateindex'].'0'));
                $group_time_ary = array();
                for($i=0;;$i++) {
                    $temp=date("H:i", $_date[$i]);
                  //  print_r($temp);
                    if($_time!==$temp){
                        $group_time_ary[$temp] = 0;
                    }
                    else{
                        break;
                    }
                }
                $group_time_ary[$_time] = $row['count'];

                while($row = $res->fetch_assoc()) {
                    $f=floor((strtotime(date("H:i",strtotime( $row['dateindex'].'0')))-strtotime(date("H:i",strtotime($_time))))%86400/600);
                    $dd = $_time;
                    $_time = date("H:i",strtotime( $row['dateindex'].'0'));
                    $group_time_ary[$_time]=$row['count'];
                    if($f>1){
                        while(--$f){
                            $tmp_str = date('H:i',strtotime("$dd+10minute"));
                            $group_time_ary[$tmp_str] = 0;
                            $dd=$tmp_str;
                        }
                    }
                }
            }
            $tt = date('H:i',strtotime("$_time+10minute"));
            $group_time_ary[$tt] = 0;
            $group[$name]=$group_time_ary;
        }
    }
} else{
    echo "没有数据";
}
$ret_data = array();
$ret_data['res'] = $group;
echo json_encode($ret_data);
$conn->close();
?>